// pages/orderrecord/index.js
import {
  Index
} from 'index-model.js'
var ajax = new Index(); // 实例化
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    payOrderInfo: [], // 支付订单信息
    payText: '微信支付(离场)',
    btnShowType: 0, // 电话显示状态
    phoneNumber: '', // 收费员电话
    prderData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let OrderItem = wx.getStorageSync("CarOrderData");
    let itemData = OrderItem.OrderDtos;
    if (itemData.length > 1) {
      this.setData({
        payText: '微信补交(离场)'
      })
    }
    if (OrderItem.ButtonShowType == null || OrderItem.ButtonShowType == 'undefind') {
      OrderItem.ButtonShowType = 0
    }
    if (OrderItem.AdminPhoneNumber == null || OrderItem.AdminPhoneNumber == 'undefind') {
      OrderItem.AdminPhoneNumber = ''
    }
    this.setData({
      btnShowType: OrderItem.ButtonShowType,
      phoneNumber: OrderItem.AdminPhoneNumber,
      payOrderInfo: itemData,
      prderData: OrderItem
    })
    console.log(this.data.prderData)
  },


  // 微信支付
  handleWxchatPay(){
    let total = parseInt(this.data.prderData.Total);
    if(total == 0){
      return
    }else{
      wx.navigateTo({
        url: '/pages/payorder/index'
      })
    }
  },


  // 拨打电话
  handleCall() {
    let that = this
    wx.showModal({
      title: '提示',
      confirmText: '拨打',
      content: '订单金额为 0元 请务必电话通知收费员离场，否则会持续计费。',
      success(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: that.data.phoneNumber
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 余额支付
  balancePayMnet: function (e) {
    let that = this;
    let userid = 'hnacyb';
    let orderType = wx.getStorageSync('orderStatusType');
    let menter = {
      PayType: 4,
      OrderDtos: that.data.payOrderInfo.data,
      AppId: userid,
      OrderType: orderType
    };
    ajax.getPayMentData(menter, (res) => {
      if (res.status == 1) {
        wx.showToast({
          title: res.message,
          success: function (res) {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/index/index'
              })
            }, 2000)
          }
        })
        wx.removeStorageSync('orderStatusType');
        wx.removeStorageSync('pathUrl');
      } else {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              //console.log('用户点击确定')
            }
          }
        })
      }
    })
  },



  // 无费用缴费
  handlePaymentNot() {
    let that = this
    let btnType = parseInt(that.data.btnShowType); // 电话显示状态
    let prderTotal = parseInt(that.data.prderData.Total); // 订单总价
    if(btnType == 1 && prderTotal == 0){
      that._showPhoneTipFun()
    }else{
      that._showTipFun()
    }
  },

  // 电话提示方法
  _showPhoneTipFun(){
    let phone = this.data.phoneNumber; // 收费员电话
    wx.showModal({
      title: '提示',
      content: '订单金额为 0元 请务必电话通知收费员离场，否则会持续计费。',
      success(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: phone
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 提示方法
  _showTipFun(){
    let that = this
    wx.showModal({
      title: '提示',
      content: '无费用离场后请驶离停车位否则系统将继续计费。',
      success(res) {
        if (res.confirm) {
          that._LeaveDataFun()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },



  // 无费用离场方法
  _LeaveDataFun() {
    let that = this;
    let id = that.data.payOrderInfo[0].Id;
    ajax.getUpdateOrderStatePayData(id, (res) => {
      if (res.status == "1") {
        wx.showToast({
          title: res.message,
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 2000)
        wx.removeStorageSync('orderStatusType');
        wx.removeStorageSync('pathUrl');
      } else {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})